package com.jml.类加载器.自定义类加载器;

import java.io.File;

public class TestMain {
    private static long startTime;

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        自定义类加载器 myClassLoader001 = new 自定义类加载器();
        myClassLoader001.setFileObject(new File("D:\\code\\OrderServiceImpl.class"));
        Class<?> aClass = myClassLoader001.findClass("com.jml.类加载器.自定义类加载器.OrderServiceImpl");
        OrderServiceImpl orderService = (OrderServiceImpl) aClass.newInstance();
        System.out.println("读取class成功：" + orderService.addOrder());

        File fileObject = myClassLoader001.getFileObject();
        startTime = fileObject.lastModified();
        while (true) {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {

            }
            long endTime = fileObject.lastModified();
            if (endTime != startTime) {

                System.out.println("class文件发生了变化，需要重新读取");
                try {
                    自定义类加载器 myClassLoader = new 自定义类加载器();
                    myClassLoader.setFileObject(new File("D:\\code\\OrderServiceImpl.class"));
                    Class<?> newClass = myClassLoader.findClass("com.jml.类加载器.自定义类加载器.OrderServiceImpl");
                    OrderServiceImpl newOrderService = (OrderServiceImpl) newClass.newInstance();
                    System.out.println("读取class成功：" + newOrderService.addOrder());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                startTime = endTime;
            }
        }
    }
}
